package cn.blunce.weather.models;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import cn.blunce.weather.models.HourlyForcast;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "HOURLY_FORCAST".
*/
public class HourlyForcastDao extends AbstractDao<HourlyForcast, Long> {

    public static final String TABLENAME = "HOURLY_FORCAST";

    /**
     * Properties of entity HourlyForcast.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Date = new Property(1, String.class, "date", false, "DATE");
        public final static Property Hum = new Property(2, Double.class, "hum", false, "HUM");
        public final static Property Pres = new Property(3, Double.class, "pres", false, "PRES");
        public final static Property Pop = new Property(4, Double.class, "pop", false, "POP");
        public final static Property Tmp = new Property(5, Double.class, "tmp", false, "TMP");
        public final static Property Wind_ID = new Property(6, Long.class, "wind_ID", false, "WIND__ID");
        public final static Property Weather_message_id = new Property(7, Long.class, "weather_message_id", false, "WEATHER_MESSAGE_ID");
    };

    private DaoSession daoSession;

    private Query<HourlyForcast> weatherMessage_Hourly_forcastsQuery;

    public HourlyForcastDao(DaoConfig config) {
        super(config);
    }
    
    public HourlyForcastDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"HOURLY_FORCAST\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"DATE\" TEXT," + // 1: date
                "\"HUM\" REAL," + // 2: hum
                "\"PRES\" REAL," + // 3: pres
                "\"POP\" REAL," + // 4: pop
                "\"TMP\" REAL," + // 5: tmp
                "\"WIND__ID\" INTEGER," + // 6: wind_ID
                "\"WEATHER_MESSAGE_ID\" INTEGER);"); // 7: weather_message_id
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"HOURLY_FORCAST\"";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, HourlyForcast entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(2, date);
        }
 
        Double hum = entity.getHum();
        if (hum != null) {
            stmt.bindDouble(3, hum);
        }
 
        Double pres = entity.getPres();
        if (pres != null) {
            stmt.bindDouble(4, pres);
        }
 
        Double pop = entity.getPop();
        if (pop != null) {
            stmt.bindDouble(5, pop);
        }
 
        Double tmp = entity.getTmp();
        if (tmp != null) {
            stmt.bindDouble(6, tmp);
        }
 
        Long wind_ID = entity.getWind_ID();
        if (wind_ID != null) {
            stmt.bindLong(7, wind_ID);
        }
 
        Long weather_message_id = entity.getWeather_message_id();
        if (weather_message_id != null) {
            stmt.bindLong(8, weather_message_id);
        }
    }

    @Override
    protected void attachEntity(HourlyForcast entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public HourlyForcast readEntity(Cursor cursor, int offset) {
        HourlyForcast entity = new HourlyForcast( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // date
            cursor.isNull(offset + 2) ? null : cursor.getDouble(offset + 2), // hum
            cursor.isNull(offset + 3) ? null : cursor.getDouble(offset + 3), // pres
            cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4), // pop
            cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5), // tmp
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // wind_ID
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7) // weather_message_id
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, HourlyForcast entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setDate(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setHum(cursor.isNull(offset + 2) ? null : cursor.getDouble(offset + 2));
        entity.setPres(cursor.isNull(offset + 3) ? null : cursor.getDouble(offset + 3));
        entity.setPop(cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4));
        entity.setTmp(cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5));
        entity.setWind_ID(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setWeather_message_id(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(HourlyForcast entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(HourlyForcast entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "hourly_forcasts" to-many relationship of WeatherMessage. */
    public List<HourlyForcast> _queryWeatherMessage_Hourly_forcasts(Long weather_message_id) {
        synchronized (this) {
            if (weatherMessage_Hourly_forcastsQuery == null) {
                QueryBuilder<HourlyForcast> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Weather_message_id.eq(null));
                weatherMessage_Hourly_forcastsQuery = queryBuilder.build();
            }
        }
        Query<HourlyForcast> query = weatherMessage_Hourly_forcastsQuery.forCurrentThread();
        query.setParameter(0, weather_message_id);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getWeatherWindDao().getAllColumns());
            builder.append(" FROM HOURLY_FORCAST T");
            builder.append(" LEFT JOIN WEATHER_WIND T0 ON T.\"WIND__ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected HourlyForcast loadCurrentDeep(Cursor cursor, boolean lock) {
        HourlyForcast entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        WeatherWind wind = loadCurrentOther(daoSession.getWeatherWindDao(), cursor, offset);
        entity.setWind(wind);

        return entity;    
    }

    public HourlyForcast loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<HourlyForcast> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<HourlyForcast> list = new ArrayList<HourlyForcast>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<HourlyForcast> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<HourlyForcast> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
